package com.ruoyi.video.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.video.domain.Video;
import com.ruoyi.video.domain.VideoRelation;
import com.ruoyi.video.service.IVideoRelationService;
import com.ruoyi.video.service.IVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 番剧关联Controller
 *
 * @author ruoyi
 * @date 2022-02-04
 */
@RestController
@RequestMapping("/video/relation")
public class VideoRelationController extends BaseController {
    @Autowired
    private IVideoRelationService videoRelationService;
    @Autowired
    private IVideoService videoService;

    /**
     * 查询番剧关联列表
     */
    @PreAuthorize("@ss.hasPermi('video:relation:list')")
    @GetMapping("/list")
    public TableDataInfo list(VideoRelation videoRelation) {
        startPage();
        List<VideoRelation> list = videoRelationService.selectVideoRelationList(videoRelation);
        return getDataTable(list);
    }

    /**
     * 导出番剧关联列表
     */
    @PreAuthorize("@ss.hasPermi('video:relation:export')")
    @Log(title = "番剧关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VideoRelation videoRelation) {
        List<VideoRelation> list = videoRelationService.selectVideoRelationList(videoRelation);
        ExcelUtil<VideoRelation> util = new ExcelUtil<VideoRelation>(VideoRelation.class);
        util.exportExcel(response, list, "番剧关联数据");
    }

    /**
     * 获取番剧关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:relation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(videoRelationService.selectVideoRelationById(id));
    }

    /**
     * 获取番剧关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:relation:query')")
    @GetMapping(value = "/getRelationVideos/{id}")
    public AjaxResult videoRelation(@PathVariable("id") String id) {
        VideoRelation videoRelation = new VideoRelation();
        videoRelation.setVideoIds(id);
        List<VideoRelation> videoRelations = videoRelationService.selectVideoRelationList(videoRelation);
        if (CollectionUtils.isEmpty(videoRelations)) return AjaxResult.success("[]");
        return AjaxResult.success(videoService.selectVideoByIds(videoRelations.get(0).getVideoIds().split(",")));
    }


    /**
     * 新增番剧关联
     */
    @PreAuthorize("@ss.hasPermi('video:relation:add')")
    @Log(title = "番剧关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VideoRelation videoRelation) {
        if (StringUtils.isEmpty(videoRelation.getVideoIds())) return AjaxResult.noHaveParam();
        String[] split = videoRelation.getVideoIds().split(",");
        int row = videoRelationService.deleteVideoRelationByRelationId(split[0]);
        if (StringUtils.isEmpty(videoRelation.getId())) videoRelation.setId(IdUtils.fastUUID());
        row += videoRelationService.insertVideoRelation(videoRelation);
        return toAjax(row);
    }

    /**
     * 修改番剧关联
     */
    @PreAuthorize("@ss.hasPermi('video:relation:edit')")
    @Log(title = "番剧关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VideoRelation videoRelation) {
        return toAjax(videoRelationService.updateVideoRelation(videoRelation));
    }

    /**
     * 删除番剧关联
     */
    @PreAuthorize("@ss.hasPermi('video:relation:remove')")
    @Log(title = "番剧关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(videoRelationService.deleteVideoRelationByIds(ids));
    }
}
